package com.gzhryc.shared_device.oem.h5.agent.mng.controllers.market;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.NumberTools;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.net_marketing.supplier.Supplier;
import com.gzhryc.net_marketing.supplier.SupplierManager;
import com.gzhryc.net_marketing.supplier.config.dao.db.SupplierLogisticsCostConfig;
import com.gzhryc.net_marketing.supplier.config.services.SupplierLogisticsCostConfigService;
import com.gzhryc.net_marketing.supplier.config.services.dto.SupplierLogisticsCostConfigSearch;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.base.enums.ELogisticsAreaScope;
import com.gzhryc.shared_device.common.CountryAreaUtils;
import com.gzhryc.shared_device.oem.h5.agent.mng.WebConstants;

public class LogisticsCostConfigController {

    static Logger log = Logger.getLogger(LogisticsCostConfigController.class);

    @Route(value = { "/market/logistics_cost/list.html" })
    public String list(HttpServletRequest request) {
        request.setAttribute("areaScopeList",ELogisticsAreaScope.values());
        return "/market/logistics_cost/list.html";
    }

    @Route(value = "/market/logistics_cost/list.data")
    public ListResult<SupplierLogisticsCostConfig> listData(HttpServletRequest request) {
        ListResult<SupplierLogisticsCostConfig> result = new ListResult<SupplierLogisticsCostConfig>(DtoConstants.NULL_DATA, "暂无数据");
        Supplier supplier = SupplierManager.getSupplier();
		if(supplier != null) {
	        SupplierLogisticsCostConfigSearch search = WebUtils.getParameter(request, SupplierLogisticsCostConfigSearch.class);
	        Integer pageNo = WebUtils.getInteger(request, "page", 1);
	        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
	
	        result.getBody().setTotal(SupplierLogisticsCostConfigService.self(supplier.getJdbcKey()).countJoinBySearch(search));
	        List<SupplierLogisticsCostConfig> data = SupplierLogisticsCostConfigService.self(supplier.getJdbcKey()).findJoinBySearch(search, pageNo, pageLimit);
	        if(data != null && data.size() > 0) {
	            result.getBody().setData(data);
	            result.setCode(DtoConstants.SUCCESS,null);
	        }
		}
        return result;
    }

    @Route(value = { "/market/logistics_cost/add.html" })
    public String add(HttpServletRequest request) {
        request.setAttribute("areaScope",ELogisticsAreaScope.Country.index());    //默认
        request.setAttribute("provinceList",CountryAreaUtils.getCountryAreaList());
        return "/market/logistics_cost/edit.html";
    }

    @Route(value = { "/market/logistics_cost/add.do" })
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"新增物流价格配置失败");
        Supplier supplier = SupplierManager.getSupplier();
		if(supplier != null) {
	        SupplierLogisticsCostConfig logisticsCostConfig = WebUtils.getParameter(request, SupplierLogisticsCostConfig.class);
	        try {
	            if(ELogisticsAreaScope.Province.index().equals(logisticsCostConfig.getAreaScope())){
	                if(StringTools.isBlank(logisticsCostConfig.getProvince())){
	                    result.setMsg("请选择对应的省份");
	                    return result;
	                }
	            }
	            Double moneyStr = WebUtils.getDouble(request,"moneyStr",0D);
	            if(moneyStr == null || moneyStr < 0 || moneyStr > 300){
	                result.setMsg("物流价格请设置在0-300元以内");
	                return result;
	            }
	            Double freeMinMoneyStr = WebUtils.getDouble(request,"freeMinMoneyStr",0D);
	            if(freeMinMoneyStr == null || freeMinMoneyStr < 0 || freeMinMoneyStr > 9999){
	                result.setMsg("免邮客单价请设置在0-9999元以内");
	                return result;
	            }
	
	            logisticsCostConfig.setMoney(NumberTools.changeMoney(moneyStr));
	            logisticsCostConfig.setFreeMinMoney(NumberTools.changeMoney(freeMinMoneyStr));
	
	            if(SupplierLogisticsCostConfigService.self(supplier.getJdbcKey()).add(logisticsCostConfig)) {
	                result.setCode(DtoConstants.SUCCESS, null);
	            }
	        } catch (LogicException e) {
	            result.setBody(e.getMessage());
	        }
		}
        return result;
    }

    @Route(value = { "/market/logistics_cost/update.html" })
    public String update(HttpServletRequest request) {
        Long id = WebUtils.getLong(request, "id");
        Supplier supplier = SupplierManager.getSupplier();
		if(supplier != null && id != null) {
	        SupplierLogisticsCostConfig logisticsCostConfig = SupplierLogisticsCostConfigService.self(supplier.getJdbcKey()).getJoinById(id);
	        WebUtils.setAttribute(request, logisticsCostConfig);
		}
        request.setAttribute("provinceList", CountryAreaUtils.getCountryAreaList());
        return "/market/logistics_cost/edit.html";
    }

    @Route(value = { "/market/logistics_cost/update.do" })
    public AjaxResult doUpdate(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"修改物流价格配置失败");
        Supplier supplier = SupplierManager.getSupplier();
        if(supplier != null) {
	        SupplierLogisticsCostConfig logisticsCostConfig = WebUtils.getParameter(request, SupplierLogisticsCostConfig.class);
	        try {
	            if(ELogisticsAreaScope.Province.index().equals(logisticsCostConfig.getAreaScope())){
	                if(StringTools.isBlank(logisticsCostConfig.getProvince())){
	                    result.setMsg("请选择对应的省份");
	                    return result;
	                }
	            }
	            Double moneyStr = WebUtils.getDouble(request,"moneyStr",0D);
	            if(moneyStr == null || moneyStr < 0 || moneyStr > 300){
	                result.setMsg("物流价格请设置在0-300元以内");
	                return result;
	            }
	            Double freeMinMoneyStr = WebUtils.getDouble(request,"freeMinMoneyStr",0D);
	            if(freeMinMoneyStr == null || freeMinMoneyStr < 0 || freeMinMoneyStr > 9999){
	                result.setMsg("免邮客单价请设置在0-9999元以内");
	                return result;
	            }
	
	            logisticsCostConfig.setMoney(NumberTools.changeMoney(moneyStr));
	            logisticsCostConfig.setFreeMinMoney(NumberTools.changeMoney(freeMinMoneyStr));
	
	            if(SupplierLogisticsCostConfigService.self(supplier.getJdbcKey()).updateNotNull(logisticsCostConfig)) {
	                result.setCode(DtoConstants.SUCCESS, null);
	            }
	        } catch (LogicException e) {
	            result.setBody(e.getMessage());
	        }
        }
        return result;
    }

    @Route(value = { "/market/logistics_cost/del.do" })
    public AjaxResult doDelete(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"删除物流价格配置失败");
        Long id = WebUtils.getLong(request, "id");
        Supplier supplier = SupplierManager.getSupplier();
        if(supplier != null) {
	        try {
	            if(SupplierLogisticsCostConfigService.self(supplier.getJdbcKey()).deleteById(id)) {
	                result.setCode(DtoConstants.SUCCESS, null);
	            }
	        } catch (LogicException e) {
	            result.setBody(e.getMessage());
	        }
        }
        return result;
    }
}
